/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.bean.PostsBean;
import model.dao.PostCategoryDAO;
import model.dao.PostDAO;
import model.dto.Post;
import model.dto.PostCategory;
import model.util.PagingUtil;
import model.util.SharingUtil;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author PhucAn
 */
public class PostsAction extends BaseAction {

    PostDAO daoPosts = new PostDAO();
    PostCategoryDAO daoPCate = new PostCategoryDAO();

    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ArrayList<PostCategory> cateList = daoPCate.getAllCategories();
        request.setAttribute("list", cateList);
        request.setAttribute(CONTENT_HOLDER, "post-add");
        return mapping.findForward("home");
    }

    public ActionForward create(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {       
        Post p = new Post();
        p.setTitle(request.getParameter("title") == null ? "" : request.getParameter("title"));
        p.setContent(request.getParameter("content") == null ? "" : request.getParameter("content"));
        p.getCategory().setId(Integer.parseInt(request.getParameter("category").equals("") ? "0" : request.getParameter("category")));
        p.setState(request.getParameter("is-public") != null);
        p.setAuthor(SharingUtil.getUser(request.getSession()));
        if (daoPosts.create(p) > 0) {
            SharingUtil.showMessage(request, new ActionMessage("create.success"));
        } else {
            SharingUtil.showMessage(request, new ActionMessage("create.failure"));
        }
        request.setAttribute(CONTENT_HOLDER, "post-manage");
        return mapping.findForward("home");
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ArrayList<PostCategory> cateList = daoPCate.getAllCategories();
        request.setAttribute("cateList", cateList);
        request.setAttribute(CONTENT_HOLDER, "post-edit");
        int id=Integer.parseInt(request.getParameter("id"));
        Post p=daoPosts.getPost(id);
        request.setAttribute("post", p);
        return mapping.findForward("home");
    }
    
    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       ArrayList<PostCategory> cateList = daoPCate.getAllCategories();
        request.setAttribute("cateList", cateList);
        Post p = new Post();
        p.setId(Integer.parseInt(request.getParameter("id")==null?"":request.getParameter("id")));
        p.setTitle(request.getParameter("title") == null ? "" : request.getParameter("title"));
        p.setContent(request.getParameter("content") == null ? "" : request.getParameter("content"));
        p.getCategory().setId(Integer.parseInt(request.getParameter("category").equals("") ? "0" : request.getParameter("category")));
        p.setState(request.getParameter("is-public") != null);
        p.setAuthor(SharingUtil.getUser(request.getSession()));        
        request.setAttribute("post", p);        
        if(daoPosts.update(p)>0){
            SharingUtil.showMessage(request, new ActionMessage(MSG_UPDATE_SUCCESS));            
        }else{
            SharingUtil.showMessage(request, new ActionMessage(MSG_UPDATE_FAILURE));
        }
        request.setAttribute(CONTENT_HOLDER, "post-edit");        
        return mapping.findForward("home");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String strId=request.getParameter("selected-item-id")==null?"":request.getParameter("selected-item-id");
        int id=Integer.parseInt(strId);
        if(daoPosts.delete(id)>0){
            SharingUtil.showMessage(request, new ActionMessage(MSG_DELETE_SUCCESS));
        }else SharingUtil.showMessage(request, new ActionMessage(MSG_DELETE_FAILURE));
            
        return mapping.findForward("list");
    }

    public ActionForward manage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int totalRecords=0;
        int pageSize=PagingUtil.CONST_PAGE_SIZE;
        int currentPage=PagingUtil.getPageNumber(request.getParameter("page"));
        ArrayList<Post> list=daoPosts.getPosts(currentPage,pageSize);
        totalRecords=daoPosts.getTotalRecords();
        request.setAttribute("PagingNav", PagingUtil.getPageNavigator(totalRecords, pageSize, currentPage, "&page="));
        request.setAttribute("list",list);
        request.setAttribute(CONTENT_HOLDER, "post-manage");
        return mapping.findForward("home");
    }
}
